﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class PropertyDal
    {
        #region PropertyDal private
        private static void SetInfo(IDataRecord reader, ref PropertyEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<PropertyEntity> categoryList)
        {
            while (reader.Read())
            {
                var o = new PropertyEntity();
                SetInfo(reader, ref o);
                categoryList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        #region PropertyDal public

        public static PropertyEntity GetById(int id)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var property = new PropertyEntity();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString,
                    DbCommon.DatabaseSchema + "Property_GetById", id);
                if (reader.Read())
                    SetInfo(reader, ref property);
                return property;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Property_GetById: {0}", ex.Message));
            }
        }

        public static PropertyEntity GetByName(string name)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var property = new PropertyEntity();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString,
                    DbCommon.DatabaseSchema + "Property_GetByName", name);
                if (reader.Read())
                    SetInfo(reader, ref property);
                return property;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Property_GetByName: {0}", ex.Message));
            }
        }

        public static List<PropertyEntity> GetListByParentId(int parentId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstProperty = new List<PropertyEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString,
                    DbCommon.DatabaseSchema + "Property_GetListByParentId", parentId);
                SetInfoList(ref reader, ref lstProperty);
                return lstProperty;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Property_GetListByParentId: {0}", ex.Message));
            }
        }

        public static List<PropertyEntity> GetListByParentIdName(int parentId, string searchType)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstProperty = new List<PropertyEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString,
                    DbCommon.DatabaseSchema + "Property_GetListByParentIdName", parentId, searchType);
                SetInfoList(ref reader, ref lstProperty);
                return lstProperty;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Property_GetListByParentIdName: {0}", ex.Message));
            }
        }

        #endregion
    }
}
